﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Customers;

namespace MpSoft.Web.AdminCP.Controls.Customer
{
    public partial class PollEdit : System.Web.UI.UserControl
    {
        #region Var

        private int itemID = 0;

        #endregion

        #region Method

        private void BindControl()
        {
            if (itemID > 0)
            {
                var obj = PollBUSS.Poll_ByID(itemID);
                if (obj != null)
                {
                    txtTitle.Text = obj.Name;
                    txtOrderSort.NumberIncremnet = obj.DisplayOrder;
                    chkIsActive.Checked = obj.Published;

                    PollAnswerControl.PollID = itemID;

                }
            }
        }

        private int Save()
        {
            Poll obj = new Poll();
            if (itemID > 0)
                obj = PollBUSS.Poll_ByID(itemID);
            obj.Name = txtTitle.Text;
            obj.DisplayOrder = txtOrderSort.NumberIncremnet;
            obj.Published = chkIsActive.Checked;
            if (itemID > 0)
                return PollBUSS.Poll_Update(obj);
            else
                return PollBUSS.Poll_Add(obj);
        }

        private void Message(string message, int type)
        {
            switch (type)
            {
                case 1:
                    MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out itemID);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }
        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=poll");
        }
        #endregion

        #region Properties

        public int ItemID { get { return itemID; } }

        #endregion

       
    }
}